package org.mozhu.zufang.guanjia.web.controller;

import org.mozhu.zufang.guanjia.entity.User;
import org.mozhu.zufang.guanjia.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserRepository userRepository;

    @PostMapping("/save")
    public Object saveUser(@RequestParam("name") String name, @RequestParam("email") String email) {
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.save(user);
        return user;
    }

    @GetMapping("/user/{id}")
    public Object getUserById(@PathVariable("id") Long id) {
        Optional<User> user = userRepository.findById(id);
        return user.get();
    }

}
